﻿using System;
using System.Collections.Generic;
using System.IO;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
using QuickTest.Core.Events;
using QuickTest.Core.TestRunner;

namespace QuickTest.Tests.Core.TestRunner
{

    #region TestRunnerStub Class

    [Serializable]
    public class TestRunnerStub : ITestRunner
    {
        public void Run(ITestPackage testPackage, ITestEvents events)
        {
            events.RaiseRunStarted(new TestRunStartedEventArgs(1));
        }
    }

    #endregion

    [TestFixture]
    public class TestLoaderTests
    {
        private ITestLoader testLoader;

        [SetUp]
        public void SetUp()
        {
            testLoader = new TestLoader(GetTestLoaderPackage());
        }

        [TearDown]
        public void TearDown()
        {
            testLoader.Dispose();
        }

        [Test]
        public void Can_Create_Test_Loader()
        {
            Assert.That(testLoader, Is.InstanceOfType(typeof (TestLoader)), "Should create a TestLoader instance");
        }

        [Test]
        public void Cannot_Load_Null_Package()
        {
            Exception ex = TestUtils.AssertThrows<ArgumentNullException>(() => new TestLoader(null), "Should throw ArgumentNullException if package is null");
            Assert.That((ex as ArgumentNullException).ParamName, Is.EqualTo("testPackage"));
        }

        [Test]
        public void Can_Run_Tests()
        {
            bool runStarted = false;

            var eventHub = new EventHub();
            eventHub.RunStarted += (sender, e) => runStarted = true;

            testLoader.Run<TestRunnerStub>(eventHub);

            Assert.That(runStarted, "Should raise run started event");
        }

        #region Test Helpers

        public static ITestPackage GetTestLoaderPackage()
        {
            string path = NUnitTestRunnerTests.GetTestAssemblyPath();

            return new TestPackage(new List<string> {path},
                                   Path.GetFileNameWithoutExtension(path), Path.GetDirectoryName(path));
        }

        #endregion
    }
}